﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Net;

namespace Awesome_FTPlib
{
    public class FTPLib
    {
        #region Public Variables

        /// <summary>
        /// IP address or hostname to connect to
        /// </summary>
        public string server;
        /// <summary>
        /// Username to login as
        /// </summary>
        public string user;
        /// <summary>
        /// Password for account
        /// </summary>
        public string pass;
        /// <summary>
        /// Port number the FTP server is listening on
        /// </summary>
        public int port;
        /// <summary>
        /// The timeout (miliseconds) for waiting on data to arrive
        /// </summary>
        public int timeout;
        public bool logging;

        public string curPath;
        #endregion

         #region Private Variables

        private string messages; // server messages
        private string responseStr; // server response if the user wants it.
        private bool passive_mode;		// #######################################
        private long bytes_total; // upload/download info if the user wants it.
        private long file_size; // gets set when an upload or download takes place
        private Socket main_sock;
        private IPEndPoint main_ipEndPoint;
        private Socket listening_sock;
        private Socket data_sock;
        private IPEndPoint data_ipEndPoint;
        private FileStream file;
        private int response;
        private string bucket;
        private string Root;


        #endregion

        public FTPLib(string server, string user, string pass, String RootFolder)
        {
            this.server = server;
            this.user = user;
            this.pass = pass;
            port = 21;
            passive_mode = true;		// #######################################
            main_sock = null;
            main_ipEndPoint = null;
            listening_sock = null;
            data_sock = null;
            data_ipEndPoint = null;
            file = null;
            bucket = "";
            bytes_total = 0;
            timeout = 10000;	// 10 seconds
            messages = "";

            Root = RootFolder;
            logging = true;
        }

        /// <summary>
        /// Connection status to the server
        /// </summary>
        public bool IsConnected
        {
            get
            {
                if (main_sock != null)
                    return main_sock.Connected;
                return false;
            }
        }
        /// <summary>
        /// Returns true if the message buffer has data in it
        /// </summary>
        public bool MessagesAvailable
        {
            get
            {
                if (messages.Length > 0)
                    return true;
                return false;
            }
        }

        public string Messages
        {
            get
            {
                string tmp = messages;
                messages = "";
                return tmp;
            }
        }

        private void SetBinaryMode(bool mode)
        {
            if (mode)
            {
                SendCommand("TYPE I");

            }
            else
            {
                SendCommand("TYPE A");

            }

            ReadResponse();
            if (response != 200)
                Fail();
        }

        public void SendCommand(string command)
        {
            Byte[] cmd = Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray());

#if (FTP_DEBUG)
            if (command.Length > 3 && command.Substring(0, 4) == "PASS")
                Console.WriteLine("\rPASS xxx");
            else
                Console.WriteLine("\r" + command);
#endif


            main_sock.Send(cmd, cmd.Length, 0);

        }
    }
}
